package com.zuo.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.zuo.listener.LoginListener;
import com.zuo.model.LoginModel;

public class LoginFilter implements  Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
	
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse)response;
		
		if("/login.do".equals(req.getServletPath())){
			chain.doFilter(request, response);
		}else{
			
			HttpSession session = req.getSession();
			boolean flag = LoginListener.sessionMap.containsValue(session);
			if(!flag){
				System.out.println("通过username没有找到session");
				String mssg = (String) session.getAttribute("mssg");
				System.out.println("mssg"+mssg);
				mssg = mssg == null ? "会话已超时，请重新登录" : mssg;
				session.setAttribute("mssg", mssg);
				req.getRequestDispatcher("login.jsp").forward(req,res); 
			}else{
				chain.doFilter(request, response);
			}		
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
